package org.webcat.electronicsubmissionplugin.wizard;

import org.webcat.electronicsubmissionplugin.model.LongRunningTask;
import java.awt.Cursor;
import javax.swing.event.ChangeListener;
import org.openide.WizardDescriptor;
import org.openide.util.HelpCtx;
import org.webcat.electronicsubmissionplugin.model.SubmitterModel;
import org.webcat.electronicsubmissionplugin.utils.BrowserLauncher;
import org.webcat.electronicsubmissionplugin.utils.Utils;

/**
 *  This panel controls the communication between the model and the visual part
 *  of the second panel.
 *
 *  @author rob44, rstodd13, samccarn
 *  @version Apr 26, 2011
 */
@SuppressWarnings("rawtypes")
public class SubmitWizardPanel2 implements WizardDescriptor.Panel {

    /**
     * The visual component that displays this panel. If you need to access the
     * component from this class, just use getComponent().
     */
    private SubmitVisualPanel2 component;
    private SubmitterModel model = SubmitterModel.getInstance();

    /**
     * Gets the help context. Unused.
     * @return the help context
     */
    public SubmitVisualPanel2 getComponent() {
        if (component == null) {
            component = new SubmitVisualPanel2();
        }
        return component;
    }

    /**
     * Gets the help context. Unused.
     * @return the help context
     */
    public HelpCtx getHelp() {
        // Show no Help button for this panel:
        return HelpCtx.DEFAULT_HELP;
        // If you have context help:
        // return new HelpCtx(SampleWizardPanel1.class);
    }

    /**
     * If set to false, next button will become unclickable.
     * Validation is performed when the user clicks next, so next must always be clickable.
     * Unused.
     * @return true
     */
    public boolean isValid() {
        // If it is always OK to press Next or Finish, then:
        return true;
        // If it depends on some condition (form filled out...), then:
        // return someCondition();
        // and when this condition changes (last form field filled in...) then:
        // fireChangeEvent();
        // and uncomment the complicated stuff below.
    }

    /**
     * Unused
     * @param l
     */
    public final void addChangeListener(ChangeListener l) {
        // unused
    }

    /**
     * Unused
     * @param l
     */
    public final void removeChangeListener(ChangeListener l) {
        // unused
    }
    /*
    private final Set<ChangeListener> listeners = new HashSet<ChangeListener>(1); // or can use ChangeSupport in NB 6.0
    public final void addChangeListener(ChangeListener l) {
    synchronized (listeners) {
    listeners.add(l);
    }
    }
    public final void removeChangeListener(ChangeListener l) {
    synchronized (listeners) {
    listeners.remove(l);
    }
    }
    protected final void fireChangeEvent() {
    Iterator<ChangeListener> it;
    synchronized (listeners) {
    it = new HashSet<ChangeListener>(listeners).iterator();
    }
    ChangeEvent ev = new ChangeEvent(this);
    while (it.hasNext()) {
    it.next().stateChanged(ev);
    }
    }
     */

    /**
     * Uses the model to read available settings.
     * @param settings
     */
    public void readSettings(Object settings) {
        component.getSubmissionStatusLabel().setText("Loading submission.");
        LongRunningTask ltr = new LongRunningTask() {

            @Override
            public void taskToRun() throws Exception {
                model.submitProject();

            }

            @Override
            public void onError(Exception ex) {
                component.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                component.getSubmissionStatusLabel().setText(model.getServerResponseMessage());
                component.getStatusIcon().setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/button_cancel.png")));
            }

            @Override
            public void onSuccessfulComplete() {
                component.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                String responseMessage = model.getServerResponseMessage();
                component.getSubmissionStatusLabel().setText(responseMessage);
                component.getStatusIcon().setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/button_check.png")));
                String response = model.getServerResponse();
                if (response != null) {
                    BrowserLauncher.openURL(Utils.createTempFile(response));
                }
            }

            @Override
            public void beforeStart() {
                component.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            }
        };
        model.runLongRunningTask(ltr);
    }

    /**
     * Uses the model to store available settings.
     * @param settings
     */
    public void storeSettings(Object settings) {
        // not used yet
    }
}
